﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.DAL;
using System.Data.Sql;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            // set ให้เริ่มที่ txtUsername
            this.txtUsername.Focus();
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!this.txtUsername.Text.ToString().Equals(""))
                {
                    this.txtPassword.Focus();
                }
                else
                {
                    ValidateSignIn();
                }
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ValidateSignIn();
            }
        }

        private void btnSignin_Click(object sender, EventArgs e)
        {
            ValidateSignIn();
        }

        protected void ValidateSignIn()
        {
            if (this.txtUsername.Text.ToString().Equals(""))
            {
                MessageBox.Show("กรุณาใส่ Username", "เข้าสู่ระบบไม่ได้");
            }
            else if (this.txtPassword.Text.ToString().Equals(""))
            {
                MessageBox.Show(this, "กรุณาใส่ Password", "เข้าสู่ระบบไม่ได้"); // (แสดงที่ไหน,ข้อความว่าอะไร,หัวเรื่องอะไร,มีปุ่มอ่ะไรบ้าง)
                this.txtPassword.Focus();
            }
            else
            {
                //ตรงนี้เอาไว้ติดต่อ database ว่า username กับ password ถูกต้องไหมถ้าถูกก็ไปหน้า Main ตาม Role ของ user
                //ถ้าไม่ถูกต้องก็โชว์ message ไปว่า username หรือ password ผิด
                EmployeeItem item = EmployeeInfoDAL.Login(this.txtUsername.Text, this.txtPassword.Text);
                if (item != null)
                {
                    switch (item.EMPLOYEE_POSITION.ToLower()) 
                    {
                        case "a":
                            {
                                frmMainAdmin main = new frmMainAdmin(item);
                                main.ShowDialog();
                            } break;
                        case "h":
                            {
                                frmMainLoanHeadEmployee main = new frmMainLoanHeadEmployee(item);
                                main.ShowDialog();
                            } break;
                        case "s":
                            {
                                frmMainLoanEmployee main = new frmMainLoanEmployee(item);
                                main.ShowDialog();
                            } break;
                        case "p":
                            {
                                frmMainPaymentEmployee main = new  frmMainPaymentEmployee(item);
                                main.ShowDialog();
                            } break;
                        case "d":
                            {
                                frmMainDebtEmployee main = new frmMainDebtEmployee(item);
                                main.ShowDialog();
                            } break;
                        case "l":
                            {
                                frmMainLigigationEmployee main = new frmMainLigigationEmployee(item);
                                main.ShowDialog();
                            } break;
                        case "b":
                            {
                                frmMainBoss main = new frmMainBoss (item);
                                main.ShowDialog();
                            } break; 
                    }
                        ResetTextBox();
                }
                else 
                {
                    MessageBox.Show("Username หรือ Password ผิด กรูณากรอกใหม่อีกครั้ง");
                    this.txtPassword.Text = "";
                    this.txtPassword.Focus();
                }
                
            }
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            ResetTextBox();
        }

        private void ResetTextBox()
        {
            this.txtUsername.Text = String.Empty;
            this.txtPassword.Text = String.Empty;
            this.txtUsername.Focus();
        }
    }
}